﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using LostHobbits.Render;
using LostHobbits.Sound;

namespace LostHobbits.Scene
{
    class Layer : ISceneObject
    {
        private List<ISceneObject> activeMembers;
        private List<ISceneObject> inactiveMembers;
        private List<Layer> childLayers;

        public Layer()
        {
            activeMembers = new List<ISceneObject>();
            inactiveMembers = new List<ISceneObject>();
            childLayers = new List<Layer>();
        }

        public void AddInactive(ISceneObject newStatic) 
        {
            inactiveMembers.Add(newStatic);
        }

        public void AddActive(ISceneObject newActive)
        {
            activeMembers.Add(newActive);
        }

        public void AddLayer(Layer newLayer)
        {
            childLayers.Add(newLayer);
        }

        #region ISceneObject Members

        public Vector2 Position
        {
            get;
            set;
        }

        public void Update(GameTime gameTime, ISoundService sound)
        {
            foreach (ISceneObject obj in activeMembers)
            {
                obj.Update(gameTime, sound);
            }
        }   

        public void BuildRenderAssembly(List<Render.RenderManager.RendationInfo> toRender, Vector2 parentPos)
        {            
            if(inactiveMembers !=  null)
                foreach (ISceneObject obj in inactiveMembers)
                    obj.BuildRenderAssembly(toRender, Position + parentPos);
            if(activeMembers != null)
                foreach (ISceneObject obj in activeMembers)
                    obj.BuildRenderAssembly(toRender, parentPos);
            if(childLayers != null)
                foreach (Layer child in childLayers)
                    child.BuildRenderAssembly(toRender, parentPos);
        }

        public void Load(ISoundService sound)
        {
            if(childLayers != null)
                foreach (Layer child in childLayers)
                    child.Load(sound);
            if(inactiveMembers != null)
                foreach (ISceneObject obj in inactiveMembers)
                    obj.Load(sound);
            if(activeMembers != null)
                foreach (ISceneObject obj in activeMembers)
                    obj.Load(sound);
        }

        #endregion

       
    }
}
